
import { BlockInputEvents, EventTouch, Layers, Node } from "cc";
import { LayerUI } from "./LayerUI";
import { ViewParams } from "./Defines";
import { UIConfig } from "./LayerManager";
import { ViewUtil } from "../../utils/ViewUtil";

const Mask :string = "common/prefab/mask"

export class LayerPopUp extends LayerUI{
    /**
     * 触摸阻止事件
     */
    protected black!:BlockInputEvents;
    /**
     * mask 半透明资源
     */
    protected mask!:Node;

    /**
     * 构造函数，设置一个layer的name
     * @param name 
     */
    constructor(name:string){
        super(name);
        this.init();
    }

    /**
     * 初始化PopUp
     */
    private init(){
        //将layer设置成2d,用2D摄像机来显示
        this.layer = Layers.Enum.UI_2D;
        //添加阻止触摸事件
        this.black = this.addComponent(BlockInputEvents);
        //初始化时默认关闭
        this.black.enabled = false;
    }

    /**
     * 显示界面
     * @param vp 
     * @returns 
     */
    protected async showUI(vp: ViewParams): Promise<boolean> {
        const r  = await super.showUI(vp);
        if (r) {
            //界面加载完成时，启用触摸非窗口区域关闭
            this.openVacancyRemove(vp.config)
            //启用触摸
            this.black.enabled = true;
        }
        return r;
    }


    protected openVacancyRemove(config:UIConfig){
        //查看当前Node是否已经存在了Touch End 的监听，
        if (!this.hasEventListener(Node.EventType.TOUCH_END,this.onTouchEnd,this)) {
            //添加监听
            this.on(Node.EventType.TOUCH_END,this.onTouchEnd,this);
        }
        if (this.mask == null) {
            //TODO: 这里作者放在了 common/prefab/mask的一个预制体，就是在项目里必须有一个这样的预制，这里后面需要修改一下 ，
            this.mask = ViewUtil.createPrefabNode(Mask);
        }
        if (config.mask) {
            //将mask 加载到界面
            this.mask.parent = this;
            //将mask 放到最低层。
            this.mask.setSiblingIndex(0);
        }
    }

    //点击窗口外的点击事件。
    private onTouchEnd(event:EventTouch,data:any):void{
        if(event.target === this){
           this.ui_nodes.forEach(item=>{
            if (item.valid && item.config.vacancy) {
                this.remove(item.config.prefab,item.config.destroy)
            }
           })
        }
    }

    //关闭窗口
    protected onCloseWindow(vp: ViewParams): void {
        super.onCloseWindow(vp);
        //当前关闭了，将触摸事件关闭，将非窗口和遮罩关闭
        this.setBlackDisable();
    }

    /**
     * 关闭触摸非窗口区域
     */
    protected setBlackDisable(){
        if (this.ui_nodes.size == 0) {
            this.black.enabled = false;
            this.closeVacancyRemove();
            this.closeMask();
        }
    }
    /**
     * 关闭触摸监听
     */
    protected closeVacancyRemove(){
        let flag  = true;
        for (const value of this.ui_nodes.values()) {
            if (value.config.vacancy) {
                flag = false;
                break;
            }
        }
        if (flag && this.hasEventListener(Node.EventType.TOUCH_END,this.onTouchEnd,this)) {
            this.off(Node.EventType.TOUCH_END,this.onTouchEnd,this);
        }
    }

    /**
     * 关闭遮罩
     * @returns 
     */
    protected closeMask(){
        if (this.mask == null) {
            return;
        }
        let flag = true;

       for (const value of this.ui_nodes.values()) {
            if (value.config.mask) {
                flag = false;
                break;
            }
       }
       if (flag) {
        this.mask.parent = null;
       }

    }

    public clear(isDestroy:boolean){
        super.clear(isDestroy);
        this.black.enabled = false;
        this.closeVacancyRemove();
        this.closeMask();
    }


}